//
//  MHFMDB+Insert.m
//  数据库的读写封装
//
//  Created by zsgjs on 2017/2/21.
//  Copyright © 2017年 zsgjs. All rights reserved.
//

#import "MHFMDB+Insert.h"
#import "FMDB.h"

@implementation MHFMDB (Insert)

/**
 *  新增记录到数据库
 */
- (bool)insert
{
    //字典单条记录更新
    __block NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@(?) values(?);", self.currentTableName];
//    NSData *saveData = [NSKeyedArchiver archivedDataWithRootObject:fields];
    
//    __block bool result;
//    [self.db.queue inDatabase:^(FMDatabase *db) {
//        result = [db executeUpdate:insertSQL, @1, @"234", saveData];
//    }];
    
    __block BOOL result;
//    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@(?) values(?);", self.currentTableName];
    if ([self.data isKindOfClass:[NSDictionary class]] && [self.data count]>0) {
        NSDictionary *dataDict = (NSDictionary *)self.data;
        [dataDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSObject * _Nonnull obj, BOOL * _Nonnull stop) {
            if (!([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSNumber class]])) {
                NSData *saveData = [NSKeyedArchiver archivedDataWithRootObject:obj];
                
                NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@(%@) values(?);", self.currentTableName, key];
                
                [self.queue inDatabase:^(FMDatabase *db) {
                    result = [db executeUpdate:insertSQL, saveData];
                    //                [db executeUpdateWithFormat:@"INSERT INTO %@ (id,columns,columns1) values(%zd,%@,?);", self.currentTableName, 5, @"s", data];
                }];
            } else {
                __block bool result;
                NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@(%@) values(?);", self.currentTableName, key];
                [self.queue inDatabase:^(FMDatabase *db) {
                    result = [db executeUpdate:insertSQL, obj];
                    //                [db executeUpdateWithFormat:@"INSERT INTO %@ (id,columns,columns1) values(%zd,%@,?);", self.currentTableName, 5, @"s", data];
                }];
            }
//
        }];
        
//        NSString *keys = [NSString stringWithFormat:@"`%@`",[[self.data allKeys] componentsJoinedByString:@"`,`"]];
//        NSString *values =[NSString stringWithFormat:@"'%@'",[[self.data allValues] componentsJoinedByString:@"','"]];
//        
//        NSString *sql = [NSString stringWithFormat:@"INSERT INTO `%@` (%@) values(%@);",self.currentTableName,keys,values];
//        return [self excuteUpdateWithSql:sql];
    }
//
//    //数组批量递归更新
    if ([self.data isKindOfClass:[NSArray class]] && [self.data count]>0) {
        __block bool alltrue = YES;
        [self.data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:[NSDictionary class]] && ![self insertData:obj]) {
                alltrue = NO;
                NSLog(@"PYFMDB UPDATE FAILED:%@",self.lastSql);
            }
        }];
        return alltrue;
    }
    //其他则为异常情况
    NSLog(@"MHFMDB ADD DATA ERROR,DATA:%@",self.data);
    
//    [self.queue inDatabase:^(FMDatabase *db) {
//        NSString *insertSQL = @"INSERT INTO ? (id,columns,columns1,names) values(?,?,?,?)";
//        BOOL ok = [db executeUpdate:insertSQL, self.currentTableName, @1, @[@1,@2], @{@"jin": @[@"34", @123], @"cheng": @{@"la": @"kao"}}, @"mou"];
//        NSLog(@"%d", ok);
//        
//    }];
    
    
    //记录到最后sql
    self.lastSql = insertSQL;
    //如果执行失败打印到前台
    if(!result){
        NSLog(@"MHFMDB UPDATE Failed: %@",insertSQL);
    }
    return false;
}

/**
 *  根据数据源执行新增数据操作
 */
- (bool)insertData:(id)data
{
    if ([data isKindOfClass:[NSDictionary class]]) {
        return [[self dataWithDict:data] insert];
    }
    if ([data isKindOfClass:[NSArray class]]) {
        return [[self dataWithArray:data] insert];
    }
    if ([data isKindOfClass:[NSData class]]) {
        return [[self datawithJson:data] insert];
    }
    NSLog(@"PYFMDB ADD DATA ERROR,DATA:%@",data);
    return NO;
}

- (BOOL)insertNewColumn:(NSString *)colName type:(NSString *)type
{
//    NSString *key = [NSString stringWithFormat:@"`%@`", colName];
    NSString *insertNewColsql = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ %@", self.currentTableName, colName, type];
    return [self excuteUpdateWithSql:insertNewColsql];
}

@end
